import 'dart:async';

import 'package:flutter/material.dart';

class CountDownBadge extends StatefulWidget {
  final DateTime startTime;
  final DateTime endTime;
  final DateTime serverTime;
  final double marginTop;
  final double fontSize;

  CountDownBadge({
    Key key,
    @required this.startTime,
    @required this.endTime,
    @required this.serverTime,
    this.marginTop = 8,
    this.fontSize = 12, // 大于12了就是商品详情页面
  }) : super(key: key);

  @override
  _CountDownBadgeState createState() => _CountDownBadgeState();
}

class _CountDownBadgeState extends State<CountDownBadge> {
  Timer _timer;
  int _count = 0;

  @override
  void initState() {
    super.initState();
    // 检查时间
    var startTime = widget.startTime;
    var endTime = widget.endTime;
    var serverTime = widget.serverTime;
    if (startTime == null || endTime == null || serverTime == null) {
      return;
    }
    // 开始倒计时
    _timer = Timer.periodic(
      Duration(seconds: 1),
      (timer) {
        setState(() {
          _count = timer.tick;
        });
      },
    );
  }

  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // 检查时间
    var startTime = widget.startTime;
    var endTime = widget.endTime;
    var serverTime = widget.serverTime;
    if (startTime == null || endTime == null || serverTime == null) {
      return Container();
    }
    // 判断有效时间之前
    if (serverTime.isBefore(startTime)) {
      // 显示倒计时
      final difference = startTime.difference(serverTime);
      final duration = Duration(seconds: difference.inSeconds - _count);
      final format = duration.toString();
      final subFormat = format.substring(0, format.indexOf('.'));
      return Container(
        padding: EdgeInsets.symmetric(vertical: 2, horizontal: 4),
        decoration: BoxDecoration(
          color: Colors.grey,
          borderRadius: widget.fontSize > 12
              ? BorderRadius.zero
              : BorderRadius.only(
                  topRight: Radius.circular(4),
                  bottomRight: Radius.circular(4),
                ),
        ),
        margin: EdgeInsets.only(top: widget.marginTop),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Icon(
              Icons.timer,
              size: widget.fontSize,
              color: Colors.white,
            ),
            SizedBox(
              width: 4,
            ),
            Text(
              subFormat,
              style: TextStyle(color: Colors.white, fontSize: widget.fontSize),
            ),
          ],
        ),
      );
    }
    // 判断有效时间内
    if (startTime.isBefore(serverTime) && endTime.isAfter(serverTime)) {
      // 显示倒计时
      final difference = endTime.difference(serverTime);
      final duration = Duration(seconds: difference.inSeconds - _count);
      final format = duration.toString();
      final subFormat = format.substring(0, format.indexOf('.'));
      return Container(
        padding: EdgeInsets.symmetric(vertical: 2, horizontal: 4),
        decoration: BoxDecoration(
          color: Theme.of(context).accentColor,
          borderRadius: widget.fontSize > 12
              ? BorderRadius.zero
              : BorderRadius.only(
                  topRight: Radius.circular(4),
                  bottomRight: Radius.circular(4),
                ),
        ),
        margin: EdgeInsets.only(top: widget.marginTop),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Icon(
              Icons.timer,
              size: widget.fontSize,
              color: Colors.white,
            ),
            SizedBox(
              width: 4,
            ),
            Text(
              subFormat,
              style: TextStyle(color: Colors.white, fontSize: widget.fontSize),
            ),
          ],
        ),
      );
    }
    return Container();
  }
}
